package com.example.tys.baseapplication.bluetooth;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;

import com.example.tys.baseapplication.NetModel.network.ClientException;

/**
 * Created by tys on 2017/10/16.
 */

public class BlueTooth {

    //如果成功启用蓝牙，您的 Activity 将会在 onActivityResult() 回调中收到 RESULT_OK 结果代码。
    // 如果由于某个错误（或用户响应“No”）而没有启用蓝牙，则结果代码为 RESULT_CANCELED。
    public void open(Activity context, int REQUEST_ENABLE_BT) throws ClientException {
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (null == mBluetoothAdapter) {
            throw new ClientException("not found device");
        } else {
            if (!mBluetoothAdapter.isEnabled()) {
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                context.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            }
        }
    }


   public void close(BluetoothAdapter mBluetoothAdapter){
        mBluetoothAdapter.disable();
   }

}
